/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package    FeatherCMS
 * @subpackage Javascript
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 *
 * @todo I agree: this is a rather ugly way to do it. There's gotta be a better way!
 */

if (!Form) var Form = {};

Form.Help = new Class(
{
	initialize: function(form, options)
	{
		this.inputs = [];
		this.options = Object.extend({className: 'jsHelp'}, options || {});
		
		$(form).getElementsBySelector('.required, .optional').each(function(p)
		{
			var label = $E('label', p);			
			if (label.hasAttribute('title'))
			{
				var help = label.getAttribute('title');
				var input = p.getElementsBySelector('input,select,textarea')[0];
				
				input.addEvent('focus', this.show.bind(this));
				input.addEvent('blur',  this.hide.bind(this));
				this.inputs[input.getAttribute('name')] = help;
				
				label.removeAttribute('title');
			}
		}, this); 
	},
	
	show: function(evt)
	{
		var input   = new Event(evt).target();
		
		this.jsHelp = new Element('div', {'class': this.options.className},
			new Element('p').setHTML(this.inputs[input.getAttribute('name')])
		).setStyles({
			position: 'absolute',
			left: input.getLeft() + input.offsetWidth  + 'px',
			top:  input.getTop()  + 'px'
		});
		
		this.fx = new Fx.Style(this.jsHelp, 'opacity').hide();
		this.jsHelp.injectInside(document.body);
		this.fx.goTo(1);
	},
	
	hide: function(evt)
	{
		var input = new Event(evt).target();
		this.jsHelp.remove.delay(210, this.jsHelp);
		this.fx.goTo(0);
	}
});